package com.easyexcel;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.easyexcel.domain.ExcelPropertyIndexModel;
import com.easyexcel.domain.MultiLineHeadExcelModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述:
 *
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2019/3/29 16:36
 **/
@RunWith(SpringRunner.class)
@SpringBootTest
public class EasyexcelDemoApplicationTestWriter {
    @Test
    public void test1() throws FileNotFoundException {
        OutputStream out = new FileOutputStream("E:\\myself\\easyexcel-demo\\src\\main\\resources\\2007.xlsx");
        try {
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            Sheet sheet1 = new Sheet(1, 0,ExcelPropertyIndexModel.class);
            writer.write(getData(), sheet1);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private List<? extends BaseRowModel> getModeldatas() {
        List<MultiLineHeadExcelModel> list = new ArrayList<>();
        MultiLineHeadExcelModel model = new MultiLineHeadExcelModel();
        model.setP1("aaa");
        model.setP2("aaa");
        model.setP3(2222);
        model.setP4(1111);
        model.setP5("aaa");
        model.setP6("aaa");
        model.setP7("aaa");
        model.setP8("aaa");
        model.setP9("aaa");
        list.add(model);
        return list;
    }

    private List<ExcelPropertyIndexModel> getData(){
        List<ExcelPropertyIndexModel> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ExcelPropertyIndexModel model = new ExcelPropertyIndexModel();
            model.setAddress("lalala");
            model.setAge(""+i+1+"");
            model.setName("wjwei_"+i);
            model.setEmail("wjw@"+i+".com");
            model.setSex("N");
            model.setHeigh("10_"+i);
            model.setLast("hha");
            list.add(model);
        }
        return list;
    }
}
